// my/my.js
// import {http} from "../../utils/ajax"
import WxValidate from "../../utils/validate.js"

wx.cloud.init({
  env:"ymq-wuoc8"
})
const db = wx.cloud.database({
  env:"ymq-wuoc8"
})
const user =db.collection('user');
// console.log(emp)
// function getRandomColor() {
//   const rgb = []
//   for (let i = 0; i < 3; ++i) {
//     let color = Math.floor(Math.random() * 256).toString(16)
//     color = color.length === 1 ? '0' + color : color
//     rgb.push(color)
//   }
//   return '#' + rgb.join('')
// }
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData:{}
  
  },
  showModal(error){
    // console.log(error)
    wx.showModal({
      content:error.msg,
      showCancel:false
    })
  },
  gotoSignup(){
     wx.navigateTo({
       url: '../signup/signup',
     })
  },

  formSubmit:function(e){
    // console.log(e)
    var formData = e.detail.value;
    console.log(formData)
    if(!this.WxValidate.checkForm(formData)){
      const error = this.WxValidate.errorList[0];
      this.showModal(error)
      return false
    }
    // console.log(formData.tel * 1)
    user.where({
      tel:formData.tel * 1
    }).get().then(res=>{
      console.log(res)
      if(res.data.length){
        console.log(formData)
        console.log(res);
        if(res.data[0].password===formData.password){
          console.log(111)
          wx.switchTab({
            url: "../home/home",
          })
          wx.setStorage({
            data: "true",
            key: 'isLogin',
          })
          wx.setStorageSync('tel',res.data[0].tel );
        }else{
          wx.showToast({
            title: "账号或密码不匹配，请重新输入",
          })
        }
      }else{
        wx.showToast({
          title: '温馨提示',
          content:"用户还未注册,请注册",
          success:(res)=>{
            if(res.confirm){
              wx.switchTab({
                url:"../signup/signup"
              })
            }else if(res.cancel){
              
            }
          }
        })
      }
    })
  },
  rest:function(e){
    console.log(e)
    this.setData({
      formData:{}
    })
  },
  initValidate() {
    const rules = {
      tel: {
        required: true,
        tel: true
      },
      password: {
        required: true,
        rangelength: [2, 8]
      }
    }
    const messages = {
      tel: {
        required: '请填写手机号',
        tel: '请填写正确的手机号'
      },
      password: {
        required: '请填写密码',
        rangelength: '输入长度在2-8的字符'
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.audioCtx = wx.createAudioContext('myAudio')
    // this.videoContext = wx.createVideoContext('myVideo')
    // console.log(this.videoContext )
    this.initValidate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})